import requests
from bs4 import BeautifulSoup

def get_douban_top250_movie_names():
    # 豆瓣电影Top 250的URL
    url = "https://movie.douban.com/top250"
    # 请求头，模拟浏览器访问
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
    }
    # 存储电影名称的列表
    movie_names = []

    # 分页获取电影信息，每页25条，共10页
    for i in range(0, 250, 25):
        params = {'start': i}
        # 发送HTTP GET请求
        response = requests.get(url, headers=headers, params=params)
        # 使用BeautifulSoup解析HTML内容
        soup = BeautifulSoup(response.text, 'lxml')
        # 查找所有包含电影名称的div标签
        divs = soup.find_all('div', class_='hd')
        for div in divs:
            # 查找a标签
            a = div.find('a')
            # 查找包含电影名称的span标签
            span = a.find('span', class_='title')
            # 将电影名称添加到列表中
            movie_names.append(span.text)
            # 打印添加的电影名称，调试用
            print(f"Added movie: {span.text}")
    return movie_names

movie_names = get_douban_top250_movie_names()
for name in movie_names:
    print(name)

